//package com.yitiao.service;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.imooc.food.orderservicemanager.dao.OrderDetailDao;
//import com.imooc.food.orderservicemanager.dto.OrderMessageDTO;
//import com.imooc.food.orderservicemanager.enummeration.OrderStatus;
//import com.imooc.food.orderservicemanager.po.OrderDetailPO;
//import com.imooc.moodymq.listener.AbstractMessageListener;
//import com.imooc.moodymq.sender.TransMessageSender;
//import com.yitiao.listener.AbstractMessageListener;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.amqp.core.Message;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//@Slf4j
//@Service
//public class OrderMessageService extends AbstractMessageListener {
//
//    @Autowired
//    private OrderDetailDao orderDetailDao;
//    @Autowired
//    private TransMessageSender transMessageSender;
//    ObjectMapper objectMapper = new ObjectMapper();
//
//
//    @Override
//    public void receiveMessage(Message message) {
//        log.info("handleMessage:message:{}", new String(message.getBody()));
//        try {
//            OrderMessageDTO orderMessageDTO = objectMapper.readValue(message.getBody(),
//                    OrderMessageDTO.class);
//            OrderDetailPO orderPO = orderDetailDao.selectOrder(orderMessageDTO.getOrderId());
//
//            switch (orderPO.getStatus()) {
//
//                case ORDER_CREATING:
//                    if (orderMessageDTO.getConfirmed() && null != orderMessageDTO.getPrice()) {
//                        orderPO.setStatus(OrderStatus.RESTAURANT_CONFIRMED);
//                        orderPO.setPrice(orderMessageDTO.getPrice());
//                        orderDetailDao.update(orderPO);
//                        transMessageSender.send(
//                                "exchange.order.deliveryman",
//                                "key.deliveryman",
//                                orderMessageDTO
//                        );
//                    } else {
//                        orderPO.setStatus(OrderStatus.FAILED);
//                        orderDetailDao.update(orderPO);
//                    }
//                    break;
//                case RESTAURANT_CONFIRMED:
//                    if (null != orderMessageDTO.getDeliverymanId()) {
//                        orderPO.setStatus(OrderStatus.DELIVERYMAN_CONFIRMED);
//                        orderPO.setDeliverymanId(orderMessageDTO.getDeliverymanId());
//                        orderDetailDao.update(orderPO);
//                        transMessageSender.send(
//                                "exchange.order.settlement",
//                                "key.settlement",
//                                orderMessageDTO
//                        );
//                    } else {
//                        orderPO.setStatus(OrderStatus.FAILED);
//                        orderDetailDao.update(orderPO);
//                    }
//                    break;
//                case DELIVERYMAN_CONFIRMED:
//                    if (null != orderMessageDTO.getSettlementId()) {
//                        orderPO.setStatus(OrderStatus.SETTLEMENT_CONFIRMED);
//                        orderPO.setSettlementId(orderMessageDTO.getSettlementId());
//                        orderDetailDao.update(orderPO);
//                        transMessageSender.send(
//                                "exchange.order.reward",
//                                "key.reward",
//                                orderMessageDTO
//                        );
//                    } else {
//                        orderPO.setStatus(OrderStatus.FAILED);
//                        orderDetailDao.update(orderPO);
//                    }
//                    break;
//                case SETTLEMENT_CONFIRMED:
//                    if (null != orderMessageDTO.getRewardId()) {
//                        orderPO.setStatus(OrderStatus.ORDER_CREATED);
//                        orderPO.setRewardId(orderMessageDTO.getRewardId());
//                        orderDetailDao.update(orderPO);
//                    } else {
//                        orderPO.setStatus(OrderStatus.FAILED);
//                        orderDetailDao.update(orderPO);
//                    }
//                    break;
//            }
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            throw new RuntimeException();
//        }
//    }
//}
